//
//  VipCardFormatView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/30.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipCardFormatView.h"
#import "SpecCollectionViewCell.h"

@interface VipCardFormatView()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,assign)NSInteger formatRow;

@end

@implementation VipCardFormatView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = BackGroundColor;
        
        self.currentSelect = 0;
        
        self.bgNameArray = [NSMutableArray arrayWithObjects:@"currencycard_card_black",@"currencycard_card_white",@"currencycard_card_yellow", nil];
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        UIView *whiteBgView = [[UIView alloc] init];
        whiteBgView.backgroundColor = CSWhiteColor;
        [scrollView addSubview:whiteBgView];
        self.whiteBgView = whiteBgView;
        
        /**
         卡面背景
         */
        UIImageView *bgView = [[UIImageView alloc] init];
        bgView.layer.cornerRadius = 4;
        bgView.layer.masksToBounds = YES;
        [whiteBgView addSubview:bgView];
        self.bgView = bgView;
        
        /**
         卡名称
         */
        UILabel *cardClassNameLabel = [[UILabel alloc] init];
        cardClassNameLabel.font = BoldBigFont;
        cardClassNameLabel.text = @"次卡类型(45)";
        cardClassNameLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:cardClassNameLabel];
        self.cardClassNameLabel = cardClassNameLabel;
        
        /**
         有效期
         */
        UILabel *validLabel = [[UILabel alloc] init];
        validLabel.font = MidFont;
        validLabel.text = @"有效期:2017-10-01至2018-10-01";
        validLabel.textColor = CSColorFromRGB(0x97969c);
        [bgView addSubview:validLabel];
        self.validLabel = validLabel;
        
        /**
         会员ID
         */
        UILabel *vipNameLabel = [[UILabel alloc] init];
        vipNameLabel.font = MidBigFont;
        vipNameLabel.text = @"疯狂的兔子";
        vipNameLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:vipNameLabel];
        self.vipNameLabel = vipNameLabel;
        
        /**
         余额
         */
        UILabel *balanceLabel = [[UILabel alloc] init];
        balanceLabel.font = SmallFont;
        balanceLabel.text = @"余额";
        balanceLabel.textColor = CSColorFromRGB(0x97969c);
        [bgView addSubview:balanceLabel];
        self.balanceLabel = balanceLabel;
        
        
        /**
         余额
         */
        UILabel *balanceDetailLabel = [[UILabel alloc] init];
        balanceDetailLabel.font = MidBigFont;
        balanceDetailLabel.text = @"余额";
        balanceDetailLabel.textColor = CSColorFromRGB(0xffffff);
        [bgView addSubview:balanceDetailLabel];
        self.balanceDetailLabel = balanceDetailLabel;;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteBgView addSubview:lineView];
        self.lineView = lineView;

        
        CardView *cardView = [[CardView alloc] init];
        [whiteBgView addSubview:cardView];
        self.cardView = cardView;
        
        /**
         选择充值规格
         */
        UILabel *choseLabelLineView = [[UILabel alloc] init];
        choseLabelLineView.textColor = ButtonBackGroundColor;
        [whiteBgView addSubview:choseLabelLineView];
        self.choseLabelLineView = choseLabelLineView;
        
        
        /**
         选择充值规格
         */
        UILabel *choseLabel = [[UILabel alloc] init];
        choseLabel.font = BoldMidBigFont;
        choseLabel.text = @"选择购卡规格";
        choseLabel.textColor = CSColorFromRGB(0x0a0a0a);
        [whiteBgView addSubview:choseLabel];
        self.choseLabel = choseLabel;
        
        
        /**
         CollectionView
         */
        UICollectionViewFlowLayout *collectViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置cell之间间距
        collectViewFlowLayout.minimumInteritemSpacing = 5;
        //设置行距
        collectViewFlowLayout.minimumLineSpacing = 10;
        collectViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        collectViewFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        CGFloat itemWith = (SCREEN_WIDTH - 45) / 2;
        //设置Item大小
        collectViewFlowLayout.itemSize = CGSizeMake(itemWith, 110);
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectViewFlowLayout];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.backgroundColor = CSClearColor;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[SpecCollectionViewCell class] forCellWithReuseIdentifier:@"SpecCollectionViewCell"];
        [whiteBgView addSubview:collectionView];
        self.collectionView = collectionView;
        
        VipCardFormatDayView *vipCardFormatDayView = [[VipCardFormatDayView alloc] init];
        [scrollView addSubview:vipCardFormatDayView];
        self.vipCardFormatDayView = vipCardFormatDayView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self);
    }];
    
    
    [self.whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.width.mas_equalTo(self.scrollView);
    }];
    
    /**
     卡背景
     */
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.whiteBgView).offset(10);
        
        make.left.mas_equalTo(self.whiteBgView).offset(13);
        
        make.right.mas_equalTo(self).offset(-13);
    }];
    
    /**
     卡名称
     */
    [self.cardClassNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.bgView).offset(20);
        
        make.left.mas_equalTo(self.bgView).offset(21);
    }];
    
    /**
     有效期
     */
    [self.validLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.cardClassNameLabel);
        
        make.top.mas_equalTo(self.cardClassNameLabel.mas_bottom).offset(15);
    }];
    
    /**
     会员ID
     */
    [self.vipNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.bgView).offset(-45);
        
        make.left.mas_equalTo(self.validLabel);
    }];
    
    /**
     余额
     */
    [self.balanceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.bgView).offset(-20);
        
        make.bottom.mas_equalTo(self.balanceDetailLabel.mas_top).offset(-10);
    }];
    
    
    /**
     余额
     */
    [self.balanceDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.vipNameLabel);
        
        make.right.mas_equalTo(self.bgView).offset(-20);
    }];
    
    [self.cardView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.whiteBgView).offset(10);
        
        make.left.mas_equalTo(self.whiteBgView).offset(13);
        
        make.right.mas_equalTo(self.whiteBgView).offset(-13);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.width.mas_equalTo(self.scrollView);
        
        if ([self.from isEqualToString:@"add"]) {
        
            make.top.mas_equalTo(self.cardView.mas_bottom).offset(5);
        }
        else{
            
            make.top.mas_equalTo(self.bgView.mas_bottom).offset(5);
        }
        
        make.height.mas_equalTo(1);
    }];
    
    /**
     选择充值规格
     */
    [self.choseLabelLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.bgView).offset(2);
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(15);
        
        make.width.mas_equalTo(10);
        
        make.height.mas_equalTo(self.choseLabel);
        
    }];
    
    
    /**
     选择充值规格
     */
    [self.choseLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.choseLabelLineView);

        make.left.mas_equalTo(self.choseLabelLineView.mas_right).offset(5);
    }];
    
    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.choseLabel.mas_bottom);
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.height.mas_equalTo(150);
        
        make.bottom.mas_equalTo(self.whiteBgView);
    }];
    
    [self.vipCardFormatDayView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.whiteBgView.mas_bottom).offset(10);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
}


-(void)setDataSource:(NSMutableArray *)dataSource{
    
    _dataSource = dataSource;
    
    if (dataSource.count == 0) {
        
        self.vipCardFormatDayView.valid = NO;
        
        self.vipCardFormatDayView.type = self.cardType;
    }
    else{
     
        //如果是期限卡的话
        if ([self.cardType intValue] == Term) {
            
            self.vipCardFormatDayView.valid = YES;
        }
        else{
            
            NSDictionary *dict = [dataSource firstObject];
            
            if (dict) {
                
                BOOL isValidityDay = [[dict objectForKey:@"IsValidityDay"] boolValue];
                
                self.vipCardFormatDayView.valid = isValidityDay;
            }
        }
    }
    [self.collectionView reloadData];
}


#pragma mark CollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count + 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SpecCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SpecCollectionViewCell" forIndexPath:indexPath];
    
    
    if (indexPath.row == self.currentSelect) {
        
        cell.isSelect = YES;
    }
    else{
        
        cell.isSelect = NO;
    }
    
    if (indexPath.row == self.dataSource.count) {
        
        cell.isOther = YES;
    }
    else{
        
        cell.isOther = NO;
    }

    if (indexPath.row < self.dataSource.count) {
        
        NSMutableDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
        
        cell.cardType = self.cardType;
        
        cell.specDict = dict;
    }
    
    cell.layer.borderColor = CSColorFromRGB(0xe6e6e6).CGColor;
    
    return cell;
}


#pragma mark UICollectionDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    self.currentSelect = indexPath.row;
    
    [self.collectionView reloadData];
    
    //如果点击的是其他
    if (indexPath.row == self.dataSource.count) {
        
        self.vipCardFormatDayView.valid = NO;
        
        self.vipCardFormatDayView.type = self.cardType;                
    }
    else{
        
        self.vipCardFormatDayView.type = self.cardType;
        
        //期限卡一定有期限
        if ([self.cardType intValue] == Term) {
            
            self.vipCardFormatDayView.valid = YES;
        }
        else{
            
            NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
            
            if (dict) {
                
                BOOL isValidityDay = [[dict objectForKey:@"IsValidityDay"] boolValue];
                
                self.vipCardFormatDayView.valid = isValidityDay;
            }
        }
    }
}

-(void)layoutSubviews{
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.vipCardFormatDayView.bottom + 15);
}

-(void)setCardType:(NSString *)cardType{
    
    _cardType = cardType;
    
    self.vipCardFormatDayView.type = cardType;
    
}

-(void)setSelectRow:(NSInteger)selectRow{
    
    self.bgView.image = [UIImage imageNamed:self.bgNameArray[selectRow % 3]];
    
    //黑色
    if (selectRow % 3 == 0) {
        
        
        /**
         卡名称
         */
        self.cardClassNameLabel.textColor = CSColorFromRGB(0xffffff);
        
        /**
         有效期
         */
        self.validLabel.textColor = CSColorFromRGB(0x97969c);
        
        /**
         会员ID
         */
        self.vipNameLabel.textColor = CSColorFromRGB(0xffffff);
        
        
        /**
         余额
         */
        
        self.balanceLabel.textColor = CSColorFromRGB(0x97969c);
        
        
        /**
         余额
         */
        self.balanceDetailLabel.textColor = CSColorFromRGB(0xffffff);
        
    }
    else{
        
        /**
         卡名称
         */
        self.cardClassNameLabel.textColor = CSColorFromRGB(0x0a0a0a);
        
        /**
         有效期
         */
        self.validLabel.textColor = CSColorFromRGB(0x7d7c7f);
        
        /**
         会员ID
         */
        self.vipNameLabel.textColor = CSColorFromRGB(0x0a0a0a);
        
        
        /**
         余额
         */
        
        self.balanceLabel.textColor = CSColorFromRGB(0x7d7c7f);
        
        
        /**
         余额
         */
        self.balanceDetailLabel.textColor = CSColorFromRGB(0x0a0a0a);
    }
}


-(void)setFrom:(NSString *)from{
    
    _from = from;
    
    if ([from isEqualToString:@"add"]) {
        
        self.cardView.hidden = NO;
        
        /**
         卡面背景
         */
        self.bgView.hidden = YES;
        
        /**
         卡名称
         */
        self.cardClassNameLabel.hidden = YES;
        
        /**
         有效期
         */
        self.validLabel.hidden = YES;;
        
        /**
         会员ID
         */
        self.vipNameLabel.hidden = YES;;
        
        /**
         余额
         */
        self.balanceLabel.hidden = YES;;
        
        
        /**
         余额
         */
        self.balanceDetailLabel.hidden = YES;
    }
    else{
        
        self.cardView.hidden = YES;                
        
        /**
         卡面背景
         */
        self.bgView.hidden = NO;
        
        /**
         卡名称
         */
        self.cardClassNameLabel.hidden = NO;
        
        /**
         有效期
         */
        self.validLabel.hidden = NO;;
        
        /**
         会员ID
         */
        self.vipNameLabel.hidden = NO;;
        
        /**
         余额
         */
        self.balanceLabel.hidden = NO;;
        
        
        /**
         余额
         */
        self.balanceDetailLabel.hidden = NO;
    }
    
    [self setConstraints];
}
@end
